// g++ -g -o redis_test redis_test.cpp -I/home/sun/libs/hiredis/include -I/home/sun/workspace/SDFS2 -I/home/sun/libs/muduo/include -L/home/sun/libs/muduo/lib -L/home/sun/libs/hiredis/lib -lmuduo_base -lmuduo_net -lhiredis -pthread

#include <sdfs/redis/SyncRedisConnector.h>
// #include <sdfs/redis/AsyncRedisConnector.h>
// #include <muduo/net/EventLoop.h>
// #include <boost/bind.hpp>

// using namespace muduo::net;
using namespace sdfs::redis;
using namespace boost;
using namespace std;

// void getCallback(redisReply *r) {
//     if (r == NULL) return;
//     printf("result : %s\n", r->str);
// }

// void async_redis_test(const char* ip, int port)
// {
// 	EventLoop loop;
// 	redisCmdCallback fn(getCallback);
// 	AsyncRedisConnector adaptor(ip, port, &loop);
// 	adaptor.cmd(fn, "SET key %s", "hello");
// 	adaptor.cmd(fn, "GET key");
// 	loop.loop();
// }

void sync_redis_test(const char* ip, int port)
{
	SyncRedisConnectorPtr conn(new SyncRedisConnector(ip, port));
	conn->connect();
	{
	char cmd[128];

	snprintf(cmd, 128, "LPUSH %s %s", "synctime", "12545887");

	conn->command(cmd);
	}
	RedisReplyPtr reply = conn->command("LRANGE synctime 0 -1");
	cout << "synctime " << reply->getStrAt(0) << endl;

	conn->command("LPUSH mylist 0");
	conn->command("LPUSH mylist 1");

	reply = conn->command("LRANGE mylist 0 -1");
	assert(reply->isArray() == true);
	cout << "result: " << endl;
	for (int i = 0; i < reply->size(); ++i)
	{
		cout << "   " << reply->getIntAt(i) << endl;
	}

	conn->command("FLUSHDB");
}

int main()
{
	char ip[] = "127.0.0.1";
	int port = 6379;
	sync_redis_test(ip, port);
	
	return 0;
}
